package cn.com.suiyi.shixingshengxian.mine.info;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.editorpage.ShareActivity;
import com.umeng.socialize.media.UMImage;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.suiyi.shixingshengxian.R;
import cn.com.suiyi.shixingshengxian.base.BaseActivity;

/**
 * Created by Administrator on 2016/7/20.
 */
public class InfoActivity extends BaseActivity {
    @InjectView(R.id.toolbar_info)
    Toolbar toolbarInfo;
    @InjectView(R.id.layout_update_info)
    RelativeLayout layoutUpdateInfo;
    @InjectView(R.id.layout_contract_info)
    RelativeLayout layoutContractInfo;
    @InjectView(R.id.layout_info)
    RelativeLayout layoutInfo;
    private Button button;

    @Override
    protected void initData() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_info;
    }

    @Override
    public void initView() {
PopupShare popupShare=new PopupShare();
        final PopupWindow popupWindow = new PopupWindow(popupShare.onCreateView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        popupWindow.setAnimationStyle(R.style.anim_menu);
        toolbarInfo.inflateMenu(R.menu.menu_share);
        toolbarInfo.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    popupWindow.showAtLocation(layoutInfo, Gravity.BOTTOM, 0, 0);
                }
                return true;
            }
        });
        popupShare.buttonCancelPopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
popupWindow.dismiss();
            }
        });
    }


    @OnClick({R.id.layout_update_info, R.id.layout_contract_info})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_update_info:
                break;
            case R.id.layout_contract_info:
                break;
        }
    }

    public class PopupShare extends PopupWindow {
        @InjectView(R.id.layout_weichat)
        RelativeLayout layoutWeichat;
        @InjectView(R.id.layout_wechatmoments)
        RelativeLayout layoutWechatmoments;
        @InjectView(R.id.layout_qq)
        RelativeLayout layoutQq;
        @InjectView(R.id.layout_sina)
        RelativeLayout layoutSina;
        @InjectView(R.id.layout_tencentweibo)
        RelativeLayout layoutTencentweibo;
        @InjectView(R.id.layout_qzone)
        RelativeLayout layoutQzone;
        @InjectView(R.id.button_cancel_popup)
        Button buttonCancelPopup;

        public View onCreateView() {
            View view = LayoutInflater.from(InfoActivity.this).inflate(R.layout.popup_share, null);
            ButterKnife.inject(this,view);
            return view;
        }

        @OnClick({R.id.layout_weichat, R.id.layout_wechatmoments, R.id.layout_qq, R.id.layout_sina, R.id.layout_tencentweibo, R.id.layout_qzone, R.id.button_cancel_popup})
        public void onClick(View view) {
            UMShareListener    umShareListener=  new UMShareListener() {
                @Override
                public void onResult(SHARE_MEDIA platform) {
                    Toast.makeText(InfoActivity.this,platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(SHARE_MEDIA platform, Throwable t) {
                    Toast.makeText(InfoActivity.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancel(SHARE_MEDIA platform) {
                    Toast.makeText(InfoActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
                }
            };
            UMImage image = new UMImage(InfoActivity.this, R.drawable.ic_launcher);
            ShareAction shareAction=new ShareAction(InfoActivity.this);
            shareAction.setCallback(umShareListener)
                    .withText("食行生鲜，全面支持IPhone、Android手机、ipad客户端、微信公众账号")
                    .withTargetUrl("http://m.34580.com/client/index?from=singlemessage&isappinstalled=1")
            .withMedia(image);

            switch (view.getId()) {
                case R.id.layout_weichat:
                    shareAction.setPlatform(SHARE_MEDIA.WEIXIN)
                            .share();
                    break;
                case R.id.layout_wechatmoments:
                    shareAction.setPlatform(SHARE_MEDIA.WEIXIN_FAVORITE)
                            .share();
                    break;
                case R.id.layout_qq:
                    shareAction.setPlatform(SHARE_MEDIA.QQ)
                            .share();
                    break;
                case R.id.layout_sina:
                    shareAction.setPlatform(SHARE_MEDIA.SINA)
                            .share();
                    break;
                case R.id.layout_tencentweibo:
                    shareAction.setPlatform(SHARE_MEDIA.TENCENT)
                            .share();
                    break;
                case R.id.layout_qzone:
                    shareAction.setPlatform(SHARE_MEDIA.QZONE).share();
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get( this ).onActivityResult( requestCode, resultCode, data);
    }
}
